import { request } from "../request";

const token = localStorage.getItem("token");

// 数据接口定义
export interface ClassStudents {
    id?: number;
    classId?: number;
    studentId?: number;
}

export interface ResponseEntity<T> {
    code: number;
    message: string;
    data: T;
    success: boolean;
}

// 加入班级
const addClassStudentsAPI = async (classStudents: ClassStudents): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-students/addClassStudents", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            'Authorization': `Bearer ${token}`
        },
        data: classStudents
    });
};

// 退出班级
const deleteClassStudentsAPI = async (classId: number, studentId: number): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-students/deleteClassStudents", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: {
            classId: classId,
            studentId: studentId
        }
    });
};

// 获取已加入班级列表信息
const getClassStudentsByClassIdAPI = async (studentId: number): Promise<ResponseEntity<ClassStudents[]>> => {
    const token = localStorage.getItem("token");
    return request(`/api/class-students/getClassStudentsByClassId?studentId=${studentId}`, {
        method: "GET",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
    });
};

// 通过邀请码加入班级
const getInClassByInviteCodeAPI = async (inviteCode: string, studentId: number): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-students/getInClassByInviteCode", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: {
            inviteCode: inviteCode,
            studentId: studentId
        }
    });
};

// 导出所有API
export {
    addClassStudentsAPI,
    deleteClassStudentsAPI,
    getClassStudentsByClassIdAPI,
    getInClassByInviteCodeAPI
};

export default {
    addClassStudentsAPI,
    deleteClassStudentsAPI,
    getClassStudentsByClassIdAPI,
    getInClassByInviteCodeAPI
};